"use strict";
exports.__esModule = true;
var base_cmd_1 = require("../cmd/base.cmd");
var index_1 = require("../code/index");
var index_2 = require("../models/index");
var index_3 = require("../services/index");
var taro_1 = require("@tarojs/taro");
var Scheduler = /** @class */ (function () {
    function Scheduler() {
        this.loadingCount = 0;
        this.cmdMap = new Map();
    }
    Object.defineProperty(Scheduler, "Instance", {
        get: function () {
            return this._instance || (this._instance = new this());
        },
        enumerable: true,
        configurable: true
    });
    Scheduler.prototype.exe = function (cmd) {
        var _this = this;
        var model = cmd.req();
        this.cmdMap.set(model.token, cmd);
        if (cmd instanceof base_cmd_1.AHttpCmd) {
            if (cmd.showLoading()) {
                taro_1["default"].showLoading({
                    title: '加载中'
                });
            }
            index_3.httpService.sendData(model)
                .then(function (res) {
                res.data.source = JSON.parse(res.data.source);
                var cmdSign = res.data;
                _this.handleWsResp(cmdSign);
                setTimeout(function () {
                    taro_1["default"].hideLoading();
                }, 300);
            }, function (reject) {
                taro_1["default"].hideLoading();
                var cmdSign = new index_2.CmdSignModel();
                cmd.afterNetErrorDo && cmd.afterNetErrorDo(cmdSign);
                if (!cmd.afterNetErrorDo) {
                    taro_1["default"].showToast({
                        title: '网络异常，请检查网络',
                        duration: 2000,
                        icon: "none"
                    });
                }
            })["catch"](function (error) {
                taro_1["default"].hideLoading();
                console.error(error);
            });
        }
    };
    Scheduler.prototype.handleWsResp = function (cmdSign) {
        var cmd = this.cmdMap.get(cmdSign.token);
        if (cmd) {
            if (cmdSign.respCode == index_1.RespCode.SUCCESS) {
                cmd.resp(cmdSign);
                cmd.afterRespDo && cmd.afterRespDo(cmd.afterRespData || cmdSign);
            }
            else {
                cmd.error(cmdSign);
                cmd.afterErrorDo && cmd.afterErrorDo(cmdSign);
                if (!cmd.afterErrorDo) {
                    taro_1["default"].showToast({
                        title: cmdSign.msg,
                        duration: 2000,
                        icon: "none"
                    });
                }
            }
        }
        this.cmdMap["delete"](cmdSign.token);
    };
    return Scheduler;
}());
var instance = Scheduler.Instance;
exports["default"] = instance;
